/**
 * 加载我的团队任务
 * @param t
 * @param id
 * @returns
 */
var loadTasks = function(t,id){
	var date = $('#date').val();
	if(id == ''){
		id = 585;
	}
	
	var url = 'wx/task/load/'+t+'/'+id;
	
	$.ajax({
        url: url,
        type: "get",
        data: {
        	start : date
        },
        success: function(data) {
        	data = eval(data);
        	var html = '';
        	var trueName = '';
        	var no = 1;
        	for(var i = 0; i < data.length; i++){
				var item = data[i];
				
				if(item.trueName != trueName){
					trueName = item.trueName;
					
					if(i != 0){
						html += '</ul>';
					}
					
					html+='<div data-am-sticky={top:40} id="'+item.username+'">';
					html+='<button class="am-btn am-btn-success am-btn-block" data-am-offcanvas>';
					html+= trueName+'</button></div>';
					html += '<ul class="am-list am-list-static am-list-border am-list-striped">';
					no = 1; 
				}
				
				
				var type = '<span class="am-badge am-badge-warning">领导分配</span>';
				if(item.type == 1){
					type = '<span class="am-badge am-badge-primary">项目任务</span>';
				}else if(item.type == 2){
					type = '<span class="am-badge am-badge-secondary">日常工作</span>';
				}else if(item.type == 3){
					type = '<span class="am-badge am-badge-success">个人计划</span>';
				}
				
				html += '<li class="am-form am-form-horizontal"><div>';
				html +='<span class="am-badge am-round">'+no+'</span>&nbsp;'+type+'&nbsp;';
				if(item.state == 2){
					html +='<span class="am-badge am-badge-danger">已延期</span>';
				}else if(item.state == 3){
					html +='<span class="am-badge am-badge-danger">按时完成</span>';
				}else if(item.state == 4){
					html +='<span class="am-badge am-badge-danger">延期完成</span>';
				}
				html+= '<br>';
				html +='计划：'+item.content+'<br>';
				if(item.result){
					html +='完成情况：'+item.result+'<br>';
				}
				if(item.finishTime){
					html +='完成时间：'+item.finishTime+'<br>';
				}
				if(item.duration){
					html +='使用时长：'+item.duration+'分钟<br>';
				}
				if(item.type != 2){
					html +='计划开始时间：'+item.start+'<br>';
					html +='计划结束时间：'+item.end+'<br>';
				}
				
				
				html +='</div></li>';
				if(i == data.length-1){
					html += '</ul>';
				}
				no++;
			};
			
			if($("#list").html()=='' && html == ''){
				html = '团队今天都休假去了...';
			}

			$("#list").append(html);
			
			if(t== 2){
				var idV = '#'+id;
				$("html,body").animate({scrollTop: $(idV).offset().top}, 1000); 
			}
        }
    });
};
/**
 * 加载我的团队组织架构
 * @param deptId
 * @returns
 */
var loadTeam = function(id){
	var url = 'wx/task/team/origanization/'+id;

	$.ajax({
        url: url,
        type: "get",
        contentType:"application/json;charset=utf-8",
    	dateType:"json",
        success: function(data) {
        	data = eval('('+data+')');
        	var html = getDepartmentHtml(data.children);
    		html += getUsersHtml(data.users);
    		$("#team").html(html);
    		
    		$('.am-li').on('click', function() {
    			$('#offcanvas-team').offCanvas('close');
    			var id = $(this).data('id');
    			var idV = '#'+id;
    			if(!$(idV).html()){
    				loadTasks(2,id);
    			}else{
    				$("html,body").animate({scrollTop: $(idV).offset().top}, 1000); 
    			}
    			
    		});
        }
	});
};
var getDepartmentHtml = function(departments){
	var html = '';
	for(var i = 0; i < departments.length; i++){
		var item = departments[i];
	
		var navId = 'nav-'+item.deptId;
		html+='<li><a class="am-cf" data-am-collapse="{target: \'#'+navId+'\'}"><span class="am-icon-building"></span>&nbsp;';
		html+= item.deptName;
		html+='<span class="am-icon-angle-right am-fr am-margin-right"></span></a>';
		
		if(item.users.length > 0 || item.children.length > 0){
			html+='<ul class="am-list am-in" id="'+navId+'">';
			html += getDepartmentHtml(item.children);
			html += getUsersHtml(item.users);
			html+='</ul>';
		}
		
		
		html+='</li>';
		
	};
	
	return html;
};
var getUsersHtml = function(users){
	var html = '';
	for(var i = 0; i < users.length; i++){
		var item = users[i];
		html+='<li class="am-li" data-id="'+item.userId+'"><a><span class="am-icon-male"></span>&nbsp;'+item.userName+'</a></li>';
	};
	
	return html;
};

$(function() {
	var deptId = $('#deptId').val();
	
	loadTasks(3,deptId);
	loadTeam(deptId);
});